package com.chuanyang.driverh.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Handler;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.utils.pick.AddressPickTask;

import java.util.Calendar;

import cn.qqtheme.framework.entity.City;
import cn.qqtheme.framework.entity.County;
import cn.qqtheme.framework.entity.Province;
import cn.qqtheme.framework.picker.DateTimePicker;
import cn.qqtheme.framework.picker.OptionPicker;



public class DialogUtil {
    private static Dialog loadingDialog;
    private static AlertDialog  alertDialog;

    /**
     * 统一的接口加载对话框
     */
    public static void showLoadingDialog(Context context,String content) {
        try {
            if (loadingDialog!=null) {
                loadingDialog.cancel();
            }

            loadingDialog = new Dialog(context, R.style.myDialog);
            loadingDialog.setContentView(R.layout.loading_fullscreen);
            RelativeLayout view = (RelativeLayout) loadingDialog.findViewById(R.id.dialog);
            ((TextView) loadingDialog.findViewById(R.id.message)).setText(content);
            view.getBackground().setAlpha(120);
            loadingDialog.setCancelable(true);// 点击返回键无效
            loadingDialog.setCanceledOnTouchOutside(true);  //点击对话框外区域 取消对话框
            loadingDialog.show();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
    public static void cancleLoadingDialog(){
        if(loadingDialog!=null){
            try {
                loadingDialog.cancel();
            } catch (Exception e) {
            }
        }
    }


    public static Dialog myDialog(Context context,  String title, String left, String right,String content,
                                 View.OnClickListener listener) {
        Dialog  dialog = new Dialog(context, R.style.myDialog);
        dialog.setContentView( R.layout.dialog_common2);
        setDialogWidth(context, dialog, 11, 13);
        TextView dialog_title = (TextView) dialog.findViewById(R.id.dialog_title);
        if(dialog_title!=null){
            if (TextUtils.isEmpty(title)) {
                dialog_title.setVisibility(View.GONE);
            } else {
                dialog_title.setVisibility(View.VISIBLE);
                dialog_title.setText(title);
            }
        }
        TextView dialog_content = (TextView) dialog.findViewById(R.id.dialog_content);
        if(dialog_content!=null)
            dialog_content.setText(content);
        Button btn_left = (Button) dialog.findViewById(R.id.dialog_btn_left);
        Button btn_right = (Button) dialog.findViewById(R.id.dialog_btn_right);
        if(left!=null)
            btn_left.setText(left);
        {
            if (TextUtils.isEmpty(right)) {
                btn_right.setVisibility(View.GONE);
            } else {
                btn_right.setVisibility(View.VISIBLE);
                btn_right.setText(right);
            }
        }
        btn_left.setOnClickListener(listener);
        btn_right.setOnClickListener(listener);
        dialog.setCanceledOnTouchOutside(true);// 设置点击其他地方能消失
        dialog.show();
        return dialog;
    }

    public static Dialog myDialogIfDismiss(Context context,  String title, String left, String right,String content,
                                  View.OnClickListener listener,boolean autoDismiss) {
        Dialog  dialog = new Dialog(context, R.style.myDialog);
        dialog.setContentView( R.layout.dialog_common2);
        setDialogWidth(context, dialog, 11, 13);
        TextView dialog_title = (TextView) dialog.findViewById(R.id.dialog_title);
        if(dialog_title!=null){
            if (TextUtils.isEmpty(title)) {
                dialog_title.setVisibility(View.GONE);
            } else {
                dialog_title.setVisibility(View.VISIBLE);
                dialog_title.setText(title);
            }
        }
        TextView dialog_content = (TextView) dialog.findViewById(R.id.dialog_content);
        if(dialog_content!=null)
            dialog_content.setText(content);
        Button btn_left = (Button) dialog.findViewById(R.id.dialog_btn_left);
        Button btn_right = (Button) dialog.findViewById(R.id.dialog_btn_right);
        if(left!=null)
            btn_left.setText(left);
        {
            if (TextUtils.isEmpty(right)) {
                btn_right.setVisibility(View.GONE);
            } else {
                btn_right.setVisibility(View.VISIBLE);
                btn_right.setText(right);
            }
        }
        btn_left.setOnClickListener(listener);
        btn_right.setOnClickListener(listener);
        dialog.setCanceledOnTouchOutside(autoDismiss);// 设置点击其他地方能消失
        dialog.show();
        return dialog;
    }


    /**
     * 设置对话框的宽度 为一定比例的手机屏幕的宽度
     * 注意:布局的id必须是ll_dialog
     * @param context
     * @param dialog
     */
    private  static void  setDialogWidth(Context context,Dialog dialog,int percent,int total){
        LinearLayout ll = (LinearLayout) dialog.findViewById(R.id.ll_dialog);
        if(ll==null)
            return;
        ll.setLayoutParams(new LayoutParams(ActivityUtil.getScreenWidthMetrics((Activity)context)*percent/total
                ,LayoutParams.WRAP_CONTENT));
    }



    public static void onOptionPicker(Activity context, final TextView view, String[] strs ){
        OptionPicker picker = new OptionPicker(context, strs);
        picker.setCycleDisable(false);
        picker.setLineVisible(true);
        picker.setShadowVisible(false);
        picker.setTextSize(14);
        picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
            @Override
            public void onOptionPicked(int index, String item) {
                view.setText(item);
            }
        });
        picker.show();
    }


    public static void cityPick(final Activity context,final TextView view) {
        AddressPickTask task = new AddressPickTask(context);
        task.setHideCounty(true);
        task.setCallback(new AddressPickTask.Callback() {
            @Override
            public void onAddressInitFailed() {
                PrintUtil.toast(context,"数据初始化失败");
            }

            @Override
            public void onAddressPicked(Province province, City city, County county) {
                view.setText(city.getAreaName());
//                showToast(province.getAreaName() + " " + city.getAreaName());
            }
        });
        task.execute("山东", "日照");
    }

    public static void onYearMonthDayTimePicker(Activity context,final TextView tv_pick_time) {
        Calendar c=Calendar.getInstance();
        int year= c.get(Calendar.YEAR);
        int month= c.get(Calendar.MONTH)+1;
        int day= c.get(Calendar.DAY_OF_MONTH);

        DateTimePicker picker = new DateTimePicker(context, DateTimePicker.HOUR_24);
        picker.setDateRangeStart(year-1, month, day);
        picker.setDateRangeEnd(year+5, month, day);
        picker.setTimeRangeStart(0, 0);
        picker.setTimeRangeEnd(23, 59);
        picker.setSelectedItem(year,month,day,8,0);

        picker.setOnDateTimePickListener(new DateTimePicker.OnYearMonthDayTimePickListener() {
            @Override
            public void onDateTimePicked(String year, String month, String day, String hour, String minute) {
                tv_pick_time.setText(year + "-" + month + "-" + day + " " + hour + ":" + minute+":"+"00");

//                showToast(year + "-" + month + "-" + day + " " + hour + ":" + minute);
            }
        });
        picker.show();
    }

    public static void cheseCarTeamOrPersonal(final Activity context, final Handler mHandler) {
        if (null == alertDialog) {
            alertDialog = new AlertDialog.Builder(context).create();
            alertDialog.show();
            alertDialog.setTitle(null);
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    // TODO Auto-generated method stub
                    alertDialog = null;
                }
            });
            Window window = alertDialog.getWindow();
            window.setGravity(Gravity.CENTER);
            window.setBackgroundDrawable(new ColorDrawable(0));
            window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            window.setContentView(R.layout.dialog_chose_carteam_personal);
            View view = window.getDecorView();
            LinearLayout ll_chose_carteam = (LinearLayout) view.findViewById(R.id.ll_chose_carteam);
            LinearLayout ll_chose_personal = (LinearLayout) view.findViewById(R.id.ll_chose_personal);
            final ImageView  iv_carteam = (ImageView) view.findViewById(R.id.iv_chose_carteam);
            final ImageView  iv_personal = (ImageView) view.findViewById(R.id.iv_chose_personal);
            TextView tv_cancle = (TextView) view.findViewById(R.id.tv_cancle);
            TextView tv_sure = (TextView) view.findViewById(R.id.tv_sure);
            tv_cancle.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    alertDialog.dismiss();
                    alertDialog = null;
                }
            });
            tv_sure.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int type=0;
                    if(iv_carteam.isSelected())type=1;
                    if(iv_personal.isSelected())type=2;
                    mHandler.sendEmptyMessage(type);
                    alertDialog.dismiss();
                    alertDialog = null;
                }
            });
            ll_chose_carteam.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    iv_carteam.setSelected(true);
                    iv_personal.setSelected(false);

                }
            });
            ll_chose_personal.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    iv_carteam.setSelected(false);
                    iv_personal.setSelected(true);
                }
            });
        }

    }

   public static void surebidpersonal(final Activity context, final Handler mHandler, final int i
    ) {
        if (null == alertDialog) {
            alertDialog = new AlertDialog.Builder(context).create();
            alertDialog.show();
            alertDialog.setTitle(null);
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

                @Override
                public void onDismiss(DialogInterface dialog) {
                    // TODO Auto-generated method stub
                    alertDialog = null;
                }
            });
            Window window = alertDialog.getWindow();
            window.setGravity(Gravity.CENTER);

            window.setBackgroundDrawable(new ColorDrawable(0));
            window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            window.setContentView(R.layout.dialog_sure_bidpersonal);
            View view = window.getDecorView();
            TextView tv_describe = (TextView) view.findViewById(R.id.tv_describe);
            TextView tv_cancle = (TextView) view.findViewById(R.id.tv_cancle);
            TextView tv_sure = (TextView) view.findViewById(R.id.tv_sure);
            //1车队  2 个人
            if (i == 1) {
                tv_describe.setText("您将以车队名义参与竞价");
            } else {
                tv_describe.setText("是否以个人身份参与竞价?");

            }
            tv_cancle.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    alertDialog.dismiss();
                    alertDialog = null;
                }
            });
            tv_sure.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    mHandler.sendEmptyMessage(i);
                    alertDialog.dismiss();
                    alertDialog = null;
                }
            });

        }

    }


  /*  public static Dialog alertDialog(Context context, String content) {
      Dialog  dialog = new Dialog(context, R.style.myDialog);
        dialog.setContentView( R.layout.dialog_alert_one_button);
        setDialogWidth(context, dialog, 11, 13);
        TextView tv_describe = (TextView) dialog.findViewById(R.id.tv_describe);
        tv_describe.setText(content);
        TextView tv_sure = (TextView) dialog.findViewById(R.id.tv_sure);
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        dialog.setCanceledOnTouchOutside(false);// 设置点击其他地方能消失
        dialog.show();
return dialog;
    }*/



    private static Dialog dialog;
    public static void dial(final Activity activityContext, final String tel) {
        dialog = myDialog(activityContext, "拨打电话", "取消", "确定", "确定拨打：" +(StringUtil.isBlank(tel)? ConstUtil.serverNum: StringUtil.handlePhoneNumShow(tel ))+ "?", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.dialog_btn_left:
                        break;
                    case R.id.dialog_btn_right:
//                        DialogUtil.dial(activityContext,tel);
                        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + (StringUtil.isBlank(tel)? ConstUtil.serverNum: tel )));
                        try {
                            if (PermissoinUtil.checkAndRequestPermission(activityContext, android.Manifest.permission.CALL_PHONE, "请给应用添加拨打电话的权限！"))
                                activityContext.startActivity(intent);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                }
                dialog.cancel();
            }
        });
    }


}